import { Column, Entity, PrimaryGeneratedColumn, BeforeInsert, OneToOne, ManyToOne ,JoinColumn,DeleteDateColumn, ManyToMany, JoinTable} from 'typeorm';
import { CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { Duty } from 'src/modules/system/duty/entities/duty.entity';
import { Dept } from 'src/modules/system/dept/entities/dept.entity';
import { Exebook } from 'src/modules/questionbank/exebook/entities/exebook.entity';

@Entity('TrainingTask')
export class Trainingtask {
 @PrimaryGeneratedColumn('uuid')
  id: string;

  //培训名称
  @Column({length: 100})
  name: string; 
  
  //是否线上培训（0-否 1-是）
  @Column({type: 'boolean', default: 0})
  isonline: boolean; 

  //是否培训考试
  @Column({type: 'boolean', default: 0})
  hasExam: boolean;

  //培训开始时间
  @Column({type: 'timestamp'})
  starttime: Date;

  //培训结束时间
  @Column({type: 'timestamp'})
  endtime: Date;

  //是否应用题库
  @Column({type: 'boolean', default: 0})
  useQuestionBank: boolean;

  //考试题目
  @Column({ type: 'json' })
  questionComposition: {
     questionTypes: {
      type: number;       // 题型 (1-4)
      count: number;      // 题目数量
    }[];
    isRandom: boolean;    // 是否随机
  };

  //关联题库表(当useQuestionBank为1时有效)
  @ManyToOne(() => Exebook)
  @JoinTable()
  exebooks: Exebook[];
  
  //培训分类
  //岗位关联
  @ManyToOne(() => Duty, (duty) => duty.Trainingtask)
  duty: Duty;


  //业务关联
  @ManyToOne(() => Dept, (dept) => dept.Trainingtask)
  dept: Dept;

  

  //发布范围
  @Column({type: 'json'})
  publishScope: Array<{
    dept?: string;        // 关联部门ID
    duty?: string;        // 关联岗位ID
  }>;

  //考试次数限制
  @Column({type: 'int', default: 1})
  examCount: number;

  // 删除标志，1为已删除，0为未删除

  @DeleteDateColumn({ name: 'deleted_at' })
  deletedAt?: Date;         


  // 培训分类
  @Column({type: 'varchar'})
  category: string;

  //及格分数
  @Column({type: 'int', default: 60})
  passScore: number;

  //不及格是否重新学习
  @Column({type: 'boolean', default: 0})
  allowRetry: boolean;

  //补考及格分数
  @Column({type: 'int', nullable: true})
  retryPassScore: number|null;

  //时间戳
  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
